<?php

/**
 * 题库管理
 */
namespace app\admin\controller;

use app\admin\model\ArticleCates;
use app\admin\model\Questions;
use app\admin\model\QuestionTag;
use app\common\controller\AdminBaseController;
use Jrk\Tree;
use think\Exception;
use think\facade\Db;
use think\facade\Route;
use think\Request;
use app\admin\model\Exams;
use app\admin\service\ExcelService;

class Exam extends AdminBaseController
{

    protected function initialize()
   {
           parent::initialize(); // TODO: Change the autogenerated stub

          $this->model = new Exams();

          //获取所有题
          $questions = new QuestionTag();
          $qTag =$questions->field('tag_id,name')->where('status != 0')->select()->toArray();
          $this->assign('qtag', $qTag);
    }

    /**
     * @return string
     * @throws \Exception
     * @author: Lucky
     * @describe:index
     */
    public function index(){

        if (IS_AJAX){
            return $this->model->getAdminPageData($this->param);
        }
        $this->assign('type', $this->param['type'] ? $this->param['type'] : 1);
        return $this->fetch();
    }

   /**
     * @param Request $request
     * @return string|\think\response\Json
     * @author:
     * @describe:添加
     */
    public function add(Request $request){
        $this->assign("info",[]);
        return $this->fetch();
    }


    /**
     * @param $id
     * @return string|\think\response\Json
     * @author:
     * @describe:编辑
     */
    public function edit($id){

        $info=$this->model->where("id",$id)->find()->toArray();
        if (!$info){
            return parent::failed("未查询到数据");
        }
        $info['tag'] = json_decode($info['tag'], true);

        $this->assign(compact("info"));
        return $this->fetch('add');
    }




    /**
     * @return string
     * @throws \Exception
     * @author: Lucky
     * @describe:回收站
     */
    public function recycle()
    {
        if (IS_AJAX) {
            return $this->model->getRecycleData($this->param);
        }
        return $this->fetch();
    }


    /**
     * @return mixed
     * @author:
     * @describe:真实删除
     */
    public function deltrue()
    {
        if (IS_AJAX) {
            $ids = $this->request->post("ids");

            return $this->model->delTrue($ids);
        }
    }


    /**
     * @return mixed
     * @author:
     * @describe:恢复数据
     */
    public function recycleData()
    {
        if (IS_AJAX) {
            $ids = $this->request->post("ids");
            return $this->model->recycleData($ids);
        }
    }

}